package controller;

import pojo.Book;
import service.BookService;
import view.BookClassification;
import view.ModifyBook;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ModifyBookListener implements ActionListener {
    private ModifyBook modifyBook;
    private BookService bookService;
    private BookClassification bookClassification;
    private int a;
    public ModifyBookListener(ModifyBook modifyBook,BookClassification bookClassification){
        this.modifyBook=modifyBook;
        this.bookClassification=bookClassification;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("返回")) {
            modifyBook.dispose();
        }
        if (e.getActionCommand().equals("修改")) {
            bookService = new BookService();
            String bookId = modifyBook.getBook().getBookId();
            String bookName = modifyBook.getTfName().getText();
            String writer = modifyBook.getTfWriter().getText();
            String page = modifyBook.getTfPage().getText();
            String price = modifyBook.getTfPrice().getText();
            String press = modifyBook.getTfPress().getText();
            String classification = (String)modifyBook.getCBType().getSelectedItem();
            String total = modifyBook.getTfTotal().getText();
            String lend = modifyBook.getTfLend().getText();
            String surplus = modifyBook.getTfSurplus().getText();
            Book book = new Book(bookId, bookName, writer, page, price, press, classification, total, lend, surplus);
            a = JOptionPane.showConfirmDialog(modifyBook, "确认修改?", "确认信息", JOptionPane.YES_NO_OPTION);
            if (a == JOptionPane.YES_OPTION) {
                bookService.update(book);
                modifyBook.dispose();
                bookClassification.refresh();
            }
        }
        if (e.getActionCommand().equals("删除")) {
            bookService = new BookService();
            a = JOptionPane.showConfirmDialog(modifyBook, "确认删除?", "确认信息", JOptionPane.YES_NO_OPTION);
            if (a == JOptionPane.YES_OPTION) {
                bookService.delete(modifyBook.getBook().getBookId());
                modifyBook.dispose();
                bookClassification.refresh();
            }
        }
    }
}
